package com.simple.datax.flow.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.MessageConstants;

/** 
 * @Title: ReadFileUtil.java 
 * @Package com.simple.datax.flow.util 
 * @Description: TODO(添加描述) 
 * @author tonney 
 * @date 2013-1-10 下午01:54:27 
 * @version V1.0 
 */
public class ReadFileUtil {
	public static Logger log = LoggerFactory.getLogger(ReadFileUtil.class);
	
	public static String readFile(){
		BufferedReader bufReader = null;
		File file = null;
		String filepath = "f:\\message.xml";
//		String encoding = "utf-8";
		String ret = "";
		try {
			file = new File(filepath);
			bufReader=new BufferedReader(new InputStreamReader(new FileInputStream(file), MessageConstants.ENCODING_UTF));
			StringBuilder stringBuilder = new StringBuilder();
			String line = null;
	        String ls = System.getProperty("line.separator");
	        while( ( line = bufReader.readLine() ) != null ) {
	            stringBuilder.append( line );
	            stringBuilder.append( ls );
	        }
	        //delete the last ls
	        stringBuilder.deleteCharAt(stringBuilder.length()-1);
	        ret = stringBuilder.toString();
	        log.info("message=>"+ret);
		} catch (FileNotFoundException e) {
			log.error("File Name - " + file.getName(), e);
		} catch (UnsupportedEncodingException e) {
			log.error("encoding - " + MessageConstants.ENCODING_UTF, e);
		} catch (IOException e) {
			log.error(e);
		} finally {
			if (bufReader != null) {
				try {
					bufReader.close();
				} catch (IOException e) {
					log.debug("", e);
				}
			}
		}
		return ret;
	}
	
	public static void main(String[] args){
		readFile();
	}
}
